<?php
namespace App\Http\Controllers\Admin;
use Illuminate\Support\Facades\DB;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;


//后台图片管理控制器
class PicController extends Controller
{

	//后台图片首页方法  GET
    public function index()
    {
		//计算数据的总条数
		$tot=DB::table('pic')->count();
		//每页展示几条数据
		$length=3;
		
		//计算总页数
		$page=ceil($tot/$length);
	
		//判断地址栏参数是否存在
		
		if(isset($_GET['page'])){
			//计算分页
			$offset=($_GET['page']-1)*$length;
			//进行数据库查询
			$data = DB::select("select * from pic order by sort asc limit $offset,$length");
			//返回json
			//return $data;

            //返回页面
            return view('admin.pic.page')->with('data',$data);

		}
	
		
		//从数据库中读取相关数据
		$data = DB::select("select * from pic order by sort asc limit 0,$length");
		//var_dump($data);



		//加载图片管理界面
          //  $jj=!empty($_GET['page'])?$_GET['page']:'1';
			return view('admin.pic.index')->with('data',$data)->with('page',$page);
    }
	
	//后台图片管理修改页面 GET
	public function edit($id){
		// 接收需要修改的数据
		//echo $id;
		$data=DB::select("select * from pic where id=?",[$id]);
	
		return view('admin.pic.edit')->with("data",$data[0]);//二维数组降维
	}
	
	
	//后台图片管理添加页面 GET
	public function create(){
		
		return view('admin.pic.create');
	}
	
	
	
	//添加图片操作  POST
	public function store(Request $request){
		//获取数据
		//dd($request->all());
		$a = DB::insert("insert into pic (id,title,img,sort) values (?,?,?,?)",[null,$request->input('title'),$request->input('img'),$request->input('sort')]);
		//判断是否插入成功
		if($a){
			//成功进行跳转
			return redirect("admin/pic");
		}else{
			//失败返回上一个页面
			return back();
		}
		
	}
	
	//修改图片操作 PUT
	public function update(Request $request,$id){
		//var_dump($request->all());
		//接收数据
		$title=$request->input('title');
		$sort=$request->input('sort');
		$img=$request->input('img');
		$oldImg=$request->input('oldImg');//接收老图
		
		$sql="update pic set title='$title',sort='$sort',img='$img' where id=$id";
		 //echo $sql;
		 if(DB::update($sql)){
			 //判断老图和新图是否一致
			 if($oldImg==$img){
				 //图片没改
			 }else{
				 if(!empty($oldImg)){
					unlink("./Uploads/Goods/".$oldImg);//删除老图
				 }
		
			 }
			 
			 return redirect('admin/pic');
		 }else{
			 return back();
		 }
		
	}
	
	//删除图片操作  DELETE
	public function destroy(Request $request){
		
		//var_dump($request->all());
		//获取删除id
		$id=$request->input('id');
		
		//查询图片
		$data=DB::select("select * from pic where id=?",[$id]);
		//var_dump($data);exit;
		//echo $data[0]->img;
		//exit;
		
		 //删除操作 1成功  0失败
		 if(DB::delete('delete from pic where id=?',[$id])){
			//删除成功,删除图片
			if(!empty($data[0]->img)){
				unlink("./Uploads/Goods/".$data[0]->img);
			}
			 
			 return '1';
		 }else{
			 return '0';
		 }
	}
	
	//删除所有的方法
	public function delAll(Request $request){
		//var_dump($request->all());
		$str = $request->input('str');
		
		$data = DB::select("select * from pic where id in ($str)");
		//var_dump($data);exit();
		
		//echo $str;
		if($a = DB::delete("delete from pic where id in ($str)")){
			//循环删除图片
			foreach ($data as $value){
				if(!empty($value->img)){
					unlink("./Uploads/Goods/".$value->img);
				}
			}
			
			return $a;
		}else{
			return 0;
		}	
	}
	
	//ajax 修改数据
	public function sort(Request $request){
		//var_dump($request->all());
	//echo "update pic set sort={$request->input('val')} where id={$request->input('id')}";
		if(DB::update("update pic set sort=? where id=?",[$request->input('val'),$request->input('id')])){
			return 1;
		}else{
			return 0;
		}
	}
	
	
}
?>